//
//  HealthThresholdsTableViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/8.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "SocketManager.h"
#import "UIColor+RGBConverHex.h"
#import "JLHealthThresholdsModel.h"
#import "HealthCustomTypeViewController.h"
#import "HealthThresholdsTableViewController.h"
//#define kCellIdentifier = @"cell"
static NSString * kCellIdentifier = @"cell";
@interface HealthThresholdsTableViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *tipTitle;
@property (strong, nonatomic) NSDictionary *dateSouce;
@property (strong, nonatomic) NSString *selectStr;
@property (strong, nonatomic) JLHealthThresholdsModel *model;
@end

@implementation HealthThresholdsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"健康阀值";
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.backgroundColor = [UIColor colorWithHex:0xf1f1f1];
    [self showNavBarWithRightTitle:nil
                    andRightAction:nil
                     andLeftAction:@selector(homeControllerBackTo)
                 andRightBtnIsShow:NO];
    [self netRequest];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _selectStr = [[NSUserDefaults standardUserDefaults] objectForKey:UserHealthSelete];
    [self.tableView reloadData];
}

- (void)setUpTableView {
    if (_tableView.delegate == nil) {
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier];
    }
    [SVProgressHUD dismiss];
    [_tableView reloadData];
}

- (void)netRequest {
    [SVProgressHUD showWithStatus:@"加载中..."];
    if ([SocketManager sharedInstance].socket.isConnected || [[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
        _selectStr = [[NSUserDefaults standardUserDefaults] objectForKey:UserHealthSelete];
        [self setUpTableView];
        _topLayout.constant = 0;
    } else {
        [InterfaceNetManager RequestHealthSettingCompletion:^(BOOL isSucceed,
                                                              NSString *message,
                                                              id data,
                                                              NSError *error) {
            
            if (isSucceed) {
                _model = [JLHealthThresholdsModel objectWithKeyValues:data];
            }
            _selectStr = [[NSUserDefaults standardUserDefaults] objectForKey:UserHealthSelete];
            [self setUpTableView];
            _topLayout.constant = -19;
        }];

    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return 2;
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
    JLLog(@"%ld,%ld",indexPath.section,indexPath.row);
  
    cell.textLabel.font = [UIFont fontWithName:kfontName size:18.f];
    cell.textLabel.textColor = [UIColor viewFlipsideBackgroundColor];
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 1)];
    topLine.backgroundColor = [UIColor colorWithHex:0xCBCACA];
    UIView *bottom = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-1, CGRectGetWidth(self.view.frame), 1)];
    topLine.backgroundColor = [UIColor colorWithHex:0xCBCACA];
    
    
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = @"房颤";
            [cell.contentView addSubview:topLine];
            [cell.contentView addSubview:bottom];
            
            break;
        case 1:
            cell.textLabel.text = @"鼾症";
            [cell.contentView addSubview:topLine];
            [cell.contentView addSubview:bottom];
            break;
        case 2:
            if (indexPath.row == 0) {
                cell.textLabel.text = @"亚健康";
                [cell.contentView addSubview:topLine];
                bottom.frame = CGRectMake(15, CGRectGetHeight(self.view.frame)-1, CGRectGetWidth(self.view.frame)-15, 1);
                [cell.contentView addSubview:bottom];
                
            } else {
                cell.textLabel.text = @"自定义";
                [cell.contentView addSubview:bottom];
            }
            break;
    }
    if (![_selectStr isEqualToString:@""]) {
        if ([_selectStr isEqualToString:cell.textLabel.text]) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 26;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                            0,
                                                            CGRectGetWidth(self.view.frame),
                                                            26)];
    
    head.backgroundColor = [UIColor colorWithHex:0xf1f1f1];
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.contentMode = UIViewContentModeScaleAspectFill;
    imageV.frame=CGRectMake(15, 4, 18, 18);
    [head addSubview:imageV];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame)+5, CGRectGetMinY(imageV.frame) + 2, 100, 18)];
    label.font = [UIFont fontWithName:kfontName size:18.f];
    label.textColor = [UIColor viewFlipsideBackgroundColor];
    [head addSubview:label];
    switch (section) {
        case 0:
        {
            [imageV setImage: [UIImage imageNamed:@"fz_xv_ico"]];
            label.text = @"心血管疾病";
        }
            break;
        case 1:
        {
            [imageV setImage: [UIImage imageNamed:@"fz_fxd"]];
            label.text = @"呼吸道疾病";
        }
            break;
        case 2:
        {
            [imageV setImage: [UIImage imageNamed:@"fz_other"]];
            label.text = @"其他方案";
        }
            break;
        default:
            break;
    }
    return head;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2 && indexPath.row == 0) {
        return;
    }
        cell.layer.shadowColor = [UIColor grayColor].CGColor;
        cell.layer.shadowOffset = CGSizeMake(0, 0.3);
        cell.layer.shadowOpacity = 0.8;
        cell.layer.masksToBounds = NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self pushHealthCustomTypeWithIndexPath:indexPath];
}

- (void)pushHealthCustomTypeWithIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    HealthCustomTypeViewController *custom = [[HealthCustomTypeViewController alloc] initWithNibName:@"HealthCustomTypeViewController" bundle:nil];
    
    if ([cell.textLabel.text isEqualToString:@"自定义"] ) {
        if ([_model.type integerValue] == 4) {
            custom.model = _model;
        }
        [custom setType:HealthTypeZDY];
        custom.title = @"自定义";
        //TODO:触发界面跳转
    } else if ([cell.textLabel.text isEqualToString:@"房颤"]) {
        if ([_model.type integerValue] == 1) {
            custom.model = _model;
        }
        custom.title = @"房颤";
        [custom setType:HealthTypeFC];
    } else if ([cell.textLabel.text isEqualToString:@"鼾症"]) {
        if ([_model.type integerValue] == 2) {
            custom.model = _model;
        }
        custom.title = @"鼾症";
        [custom setType:HealthTypeHZ];
    } else {
        //亚健康
        if ([_model.type integerValue] == 3) {
            custom.model = _model;
        }
        custom.title = @"亚健康";
        [custom setType:HealthTypeYJZ];
    }
    [self.navigationController pushViewController:custom animated:YES];
}

@end
